]> git.saurik.com Git - bison.git/blame - data/c++.m4
variants: prohibit simple copies
[bison.git] / data / c++.m4
CommitLineData
08af01c2 1 -*- Autoconf -*-
cf147260
AD
2
3# C++ skeleton for Bison
4
34136e65 5# Copyright (C) 2002-2012 Free Software Foundation, Inc.
cf147260 6
f16b0819 7# This program is free software: you can redistribute it and/or modify
cf147260 8# it under the terms of the GNU General Public License as published by
f16b0819 9# the Free Software Foundation, either version 3 of the License, or
cf147260 10# (at your option) any later version.
f16b0819 11#
cf147260
AD
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
f16b0819 16#
cf147260 17# You should have received a copy of the GNU General Public License
f16b0819 18# along with this program. If not, see <http://www.gnu.org/licenses/>.
cf147260 19
3953ed88
AD
20m4_include(b4_pkgdatadir/[c.m4])
21
cf147260
AD
22## ---------------- ##
23## Default values. ##
24## ---------------- ##
25
a4e25e1d 26b4_percent_define_default([[parser_class_name]], [[parser]])
7789b6e3
AD
27
28# Don't do that so that we remember whether we're using a user
29# request, or the default value.
30#
db8ab2be 31# b4_percent_define_default([[api.location.type]], [[location]])
7789b6e3 32
a4e25e1d 33b4_percent_define_default([[filename_type]], [[std::string]])
171ad99d 34b4_percent_define_default([[api.namespace]], m4_defn([b4_prefix]))
67501061 35
c1d19e10 36b4_percent_define_default([[global_tokens_and_yystype]], [[false]])
a4e25e1d
JD
37b4_percent_define_default([[define_location_comparison]],
38 [m4_if(b4_percent_define_get([[filename_type]]),
c1d19e10 39 [std::string], [[true]], [[false]])])
cf147260
AD
40
41
67501061 42
793fbca5
JD
43## ----------- ##
44## Namespace. ##
45## ----------- ##
46
67501061
AD
47m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])])
48
793fbca5
JD
49
50# Don't permit an empty b4_namespace_ref. Any `::parser::foo' appended to it
51# would compile as an absolute reference with `parser' in the global namespace.
52# b4_namespace_open would open an anonymous namespace and thus establish
53# internal linkage. This would compile. However, it's cryptic, and internal
54# linkage for the parser would be specified in all translation units that
55# include the header, which is always generated. If we ever need to permit
56# internal linkage somehow, surely we can find a cleaner approach.
57m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [],
67501061 58[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
59 [[namespace reference is empty]])])
60
61# Instead of assuming the C++ compiler will do it, Bison should reject any
62# invalid b4_namepsace_ref that would be converted to a valid
63# b4_namespace_open. The problem is that Bison doesn't always output
64# b4_namespace_ref to uncommented code but should reserve the ability to do so
65# in future releases without risking breaking any existing user grammars.
66# Specifically, don't allow empty names as b4_namespace_open would just convert
67# those into anonymous namespaces, and that might tempt some users.
68m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [],
67501061 69[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
70 [[namespace reference has consecutive "::"]])])
71m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [],
67501061 72[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
73 [[namespace reference has a trailing "::"]])])
74
75m4_define([b4_namespace_open],
67501061 76[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
793fbca5
JD
77[namespace ]m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref),
78 [^\(.\)[ ]*::], [\1])),
79 [::], [ { namespace ])[ {]])])
80
81m4_define([b4_namespace_close],
67501061 82[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
4977e0a7 83m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]),
b987342b 84 [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*],
793fbca5 85 [\1])),
b987342b 86 [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])])
793fbca5
JD
87
88
e3990e3c
AD
89# b4_token_enums
90# --------------
c095d689 91# Output the definition of the tokens as enums.
cf147260 92m4_define([b4_token_enums],
e3990e3c
AD
93[[enum yytokentype
94 {
95 ]m4_join([,
96 ],
97 b4_symbol_map([b4_token_enum]))[
98 };]dnl
c095d689 99])
cf147260
AD
100
101
ef51bfa7
AD
102
103
cf147260
AD
104## ----------------- ##
105## Semantic Values. ##
106## ----------------- ##
107
b9e4eb5b
AD
108# b4_semantic_type_declare
109# ------------------------
110# Declare semantic_type.
111m4_define([b4_semantic_type_declare],
112[ /// Symbol semantic values.
113m4_ifdef([b4_stype],
114[ union semantic_type
115 {b4_user_stype
116 };],
117[m4_if(b4_tag_seen_flag, 0,
118[[ typedef int semantic_type;]],
5458913a 119[[ typedef ]b4_api_PREFIX[STYPE semantic_type;]])])])
b9e4eb5b
AD
120
121
4f84717d
AD
122# b4_public_types_declare
123# -----------------------
124# Define the public types: token, semantic value, location, and so forth.
1d6b689b 125# Depending on %define token_lex, may be output in the header or source file.
4f84717d 126m4_define([b4_public_types_declare],
5458913a 127[[#ifndef ]b4_api_PREFIX[STYPE
4f84717d
AD
128]b4_semantic_type_declare[
129#else
5458913a 130 typedef ]b4_api_PREFIX[STYPE semantic_type;
4f84717d
AD
131#endif]b4_locations_if([
132 /// Symbol locations.
f6b561d9 133 typedef b4_percent_define_get([[api.location.type]],
7789b6e3 134 [[location]]) location_type;])[
1d6b689b 135
ff601366
AD
136 /// Syntax errors thrown from user actions.
137 struct syntax_error : std::runtime_error
138 {
139 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m);]b4_locations_if([
140 location_type location;])[
141 };
142
4f84717d
AD
143 /// Tokens.
144 struct token
145 {
e3990e3c 146 ]b4_token_enums[
4f84717d 147 };
1d6b689b 148
4f84717d
AD
149 /// Token type.
150 typedef token::yytokentype token_type;
1d6b689b
AD
151
152 /// A complete symbol, with its type.
153 template <typename Exact>
154 struct symbol_base_type
155 {
156 /// Default constructor.
157 inline symbol_base_type ();
158
159 /// Constructor.]b4_locations_if([
ee52f6ec 160 inline symbol_base_type (const location_type& l);])[
710c4a65 161 inline symbol_base_type (]b4_join(
1d6b689b
AD
162 [const semantic_type& v],
163 b4_locations_if([const location_type& l]))[);
164
165 /// Return this with its exact type.
166 const Exact& self () const;
167 Exact& self ();
168
169 /// Return the type of this symbol.
170 int type_get () const;
171
172 /// The semantic value.
173 semantic_type value;]b4_locations_if([
174
175 /// The location.
176 location_type location;])[
177 };
178
179 /// External form of a symbol: its type and attributes.
180 struct symbol_type : symbol_base_type<symbol_type>
181 {
182 /// The parent class.
183 typedef symbol_base_type<symbol_type> super_type;
184
185 /// Default constructor.
186 inline symbol_type ();
187
7be08dfb
AD
188 /// Destructive move, \a s is emptied.
189 inline void move (symbol_type& s);
190
2b08bceb 191 /// Constructor for tokens with semantic value.
710c4a65 192 inline symbol_type (]b4_join([token_type t],
1d6b689b
AD
193 [const semantic_type& v],
194 b4_locations_if([const location_type& l]))[);
195
2b08bceb 196 /// Constructor for valueless tokens.
710c4a65 197 inline symbol_type (]b4_join([token_type t],
1d6b689b
AD
198 b4_locations_if([const location_type& l]))[);
199
200 /// The symbol type.
201 int type;
202
2b08bceb 203 /// The symbol type.
1d6b689b
AD
204 inline int type_get_ () const;
205
2b08bceb 206 /// The token.
1d6b689b
AD
207 inline token_type token () const;
208 };
0623bacc 209]b4_symbol_constructor_declare])
4f84717d
AD
210
211
1d6b689b
AD
212# b4_public_types_define
213# ----------------------
214# Provide the implementation needed by the public types.
215m4_define([b4_public_types_define],
ff601366
AD
216[[ inline
217 ]b4_parser_class_name[::syntax_error::syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
218 : std::runtime_error (m)]b4_locations_if([
219 , location (l)])[
220 {}
221
222 // symbol_base_type.
1d6b689b 223 template <typename Exact>
ff601366 224 inline
1d6b689b
AD
225 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type ()
226 : value()]b4_locations_if([
227 , location()])[
228 {
229 }]b4_locations_if([[
230
231 template <typename Exact>
29660062 232 inline
1d6b689b
AD
233 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (const location_type& l)
234 : value()
235 , location(l)
236 {
237 }]])[
238
239 template <typename Exact>
29660062 240 inline
710c4a65 241 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (]b4_join(
1d6b689b
AD
242 [const semantic_type& v],
243 b4_locations_if([const location_type& l]))[)
244 : value(v)]b4_locations_if([
245 , location(l)])[
246 {
247 }
248
249 template <typename Exact>
29660062 250 inline
1d6b689b
AD
251 const Exact&
252 ]b4_parser_class_name[::symbol_base_type<Exact>::self () const
253 {
254 return static_cast<const Exact&>(*this);
255 }
256
257 template <typename Exact>
29660062 258 inline
1d6b689b
AD
259 Exact&
260 ]b4_parser_class_name[::symbol_base_type<Exact>::self ()
261 {
262 return static_cast<Exact&>(*this);
263 }
264
265 template <typename Exact>
29660062 266 inline
1d6b689b
AD
267 int
268 ]b4_parser_class_name[::symbol_base_type<Exact>::type_get () const
269 {
270 return self ().type_get_ ();
271 }
272
273 // symbol_type.
29660062 274 inline
1d6b689b
AD
275 ]b4_parser_class_name[::symbol_type::symbol_type ()
276 : super_type ()
277 , type ()
278 {
279 }
280
29660062 281 inline
710c4a65 282 ]b4_parser_class_name[::symbol_type::symbol_type (]b4_join(
2b08bceb 283 [token_type t],
1d6b689b
AD
284 b4_locations_if([const location_type& l]))[)
285 : super_type (]b4_locations_if([l])[)
2b08bceb 286 , type (yytranslate_ (t))
1d6b689b
AD
287 {
288 }
289
29660062 290 inline
710c4a65 291 ]b4_parser_class_name[::symbol_type::symbol_type (]b4_join(
2b08bceb 292 [token_type t],
1d6b689b
AD
293 [const semantic_type& v],
294 b4_locations_if([const location_type& l]))[)
295 : super_type (v]b4_locations_if([, l])[)
2b08bceb 296 , type (yytranslate_ (t))
1d6b689b
AD
297 {
298 }
299
7be08dfb
AD
300 inline
301 void
302 ]b4_parser_class_name[::symbol_type::move (symbol_type& s)
303 {
304 ]b4_variant_if([b4_symbol_variant([[s.type]], [value], [build], [s.value])],
305 [value = s.value;])[
306 type = s.type;]b4_locations_if([
307 location = s.location;])[
308 }
309
29660062 310 inline
1d6b689b
AD
311 int
312 ]b4_parser_class_name[::symbol_type::type_get_ () const
313 {
314 return type;
315 }
e36ec1f4 316]b4_token_ctor_if([[
29660062 317 inline
1d6b689b
AD
318 ]b4_parser_class_name[::token_type
319 ]b4_parser_class_name[::symbol_type::token () const
320 {
321 // YYTOKNUM[NUM] -- (External) token number corresponding to the
322 // (internal) symbol number NUM (which must be that of a token). */
323 static
324 const ]b4_int_type_for([b4_toknum])[
325 yytoken_number_[] =
326 {
327 ]b4_toknum[
328 };
329 return static_cast<token_type> (yytoken_number_[type]);
330 }
0623bacc
AD
331]])[]dnl
332b4_symbol_constructor_define])
333
334
335# b4_symbol_constructor_declare
336# b4_symbol_constructor_define
337# -----------------------------
338# Declare/define symbol constructors for all the value types.
339# Use at class-level. Redefined in variant.hh.
340m4_define([b4_symbol_constructor_declare], [])
341m4_define([b4_symbol_constructor_define], [])
342
1d6b689b 343
cbf25ce7
AD
344# b4_yytranslate_define
345# ---------------------
346# Define yytranslate_. Sometimes used in the header file,
347# sometimes in the cc file.
348m4_define([b4_yytranslate_define],
349[[ // Symbol number corresponding to token number t.
350 ]b4_parser_class_name[::token_number_type
e36ec1f4 351 ]b4_parser_class_name[::yytranslate_ (]b4_token_ctor_if([token_type],
cbf25ce7
AD
352 [int])[ t)
353 {
354 static
355 const token_number_type
356 translate_table[] =
357 {
358]b4_translate[
359 };
360 const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
361 const token_number_type undef_token_ = ]b4_undef_token_number[;
362
363 if (static_cast<int>(t) <= yyeof_)
364 return yyeof_;
365 else if (static_cast<unsigned int> (t) <= user_token_number_max_)
366 return translate_table[t];
367 else
368 return undef_token_;
369 }
370]])
371
1d6b689b 372
cf147260
AD
373# b4_lhs_value([TYPE])
374# --------------------
375# Expansion of $<TYPE>$.
376m4_define([b4_lhs_value],
1fa5d8bb 377[b4_symbol_value([yyval], [$1])])
cf147260
AD
378
379
380# b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
381# --------------------------------------
382# Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
383# symbols on RHS.
384m4_define([b4_rhs_value],
1fa5d8bb
AD
385[b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
386
cf147260
AD
387
388# b4_lhs_location()
389# -----------------
390# Expansion of @$.
391m4_define([b4_lhs_location],
392[(yyloc)])
393
394
395# b4_rhs_location(RULE-LENGTH, NUM)
396# ---------------------------------
397# Expansion of @NUM, where the current rule has RULE-LENGTH symbols
398# on RHS.
399m4_define([b4_rhs_location],
d1ff7a7c 400[(yylocation_stack_@{($1) - ($2)@})])
cf147260
AD
401
402
403# b4_parse_param_decl
404# -------------------
405# Extra formal arguments of the constructor.
406# Change the parameter names from "foo" into "foo_yyarg", so that
407# there is no collision bw the user chosen attribute name, and the
408# argument name in the constructor.
409m4_define([b4_parse_param_decl],
410[m4_ifset([b4_parse_param],
411 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
412
413m4_define([b4_parse_param_decl_1],
414[$1_yyarg])
415
416
417
418# b4_parse_param_cons
419# -------------------
420# Extra initialisations of the constructor.
421m4_define([b4_parse_param_cons],
422 [m4_ifset([b4_parse_param],
e9690142 423 [
cf147260
AD
424 b4_cc_constructor_calls(b4_parse_param)])])
425m4_define([b4_cc_constructor_calls],
e9690142 426 [m4_map_sep([b4_cc_constructor_call], [,
cf147260
AD
427 ], [$@])])
428m4_define([b4_cc_constructor_call],
e9690142 429 [$2 ($2_yyarg)])
cf147260
AD
430
431# b4_parse_param_vars
432# -------------------
433# Extra instance variables.
434m4_define([b4_parse_param_vars],
435 [m4_ifset([b4_parse_param],
e9690142 436 [
cf147260
AD
437 /* User arguments. */
438b4_cc_var_decls(b4_parse_param)])])
439m4_define([b4_cc_var_decls],
e9690142 440 [m4_map_sep([b4_cc_var_decl], [
cf147260
AD
441], [$@])])
442m4_define([b4_cc_var_decl],
e9690142 443 [ $1;])
426903aa
AD
444
445
446## ---------##
447## Values. ##
448## ---------##
449
450# b4_yylloc_default_define
451# ------------------------
452# Define YYLLOC_DEFAULT.
453m4_define([b4_yylloc_default_define],
454[[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
455 If N is 0, then set CURRENT to the empty location which ends
456 the previous symbol: RHS[0] (always defined). */
457
458# ifndef YYLLOC_DEFAULT
459# define YYLLOC_DEFAULT(Current, Rhs, N) \
460 do \
461 if (N) \
462 { \
463 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
464 (Current).end = YYRHSLOC (Rhs, N).end; \
465 } \
466 else \
467 { \
468 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
469 } \
470 while (/*CONSTCOND*/ false)
471# endif
472]])